﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class admin_module_form_article : System.Web.UI.UserControl
{
    Topic_BU tp_BU = new Topic_BU();
    categoryBus car_BU = new categoryBus();
    languageBus lang_BU = new languageBus();
    Article_BU art_BU = new Article_BU();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            loadTopic(drlTopic);
            loadCategory(drlCategory);
            loadLanguage(drlLang);
            loadArticleByID();
        }
    }
    public void loadTopic(DropDownList drl)
    {
        DataTable dt = new DataTable();
        dt = tp_BU.Get_All_Topic_BU();
        drl.DataSource = dt;
        drl.DataTextField = dt.Columns["topic_name"].ColumnName.ToString();
        drl.DataValueField = dt.Columns["topic_id"].ColumnName.ToString();
        drl.DataBind();
    }

    public void loadCategory(DropDownList drl)
    {
        DataTable dt = new DataTable();
        dt = car_BU.getAllCategory();
        drl.DataSource = dt;
        drl.DataTextField = dt.Columns["name"].ColumnName.ToString();
        drl.DataValueField = dt.Columns["category_id"].ColumnName.ToString();
        drl.DataBind();
    }

    public void loadLanguage(DropDownList drl)
    {
        DataTable dt = new DataTable();
        dt = lang_BU.getAllLanguage();
        drl.DataSource = dt;
        drl.DataTextField = dt.Columns["name"].ColumnName.ToString();
        drl.DataValueField = dt.Columns["language_id"].ColumnName.ToString();
        drl.DataBind();
    }

    public void loadArticleByID() {
        int id = 0;
        if (Request.QueryString["path"] != "")
        {
            id = Convert.ToInt32(Request.QueryString["path"]);
            DataTable dt = art_BU.Get_Article_By_Id_BU(id);
            foreach (DataRow dr in dt.Rows)
            {
                txtNameArticle.Text = dr["article_name"].ToString();
                txtSumery.Text = dr["article_sumary"].ToString();
                txtAuthor.Text = dr["author"].ToString();
                fckArticle.Value=dr["article_des"].ToString();
                drlCategory.Items.FindByValue(dr["category_id"].ToString()).Selected = true;
                drlLang.Items.FindByValue(dr["language_id"].ToString()).Selected = true;
                drlTopic.Items.FindByValue(dr["topic_id"].ToString()).Selected = true;
            
                if (dr["status"] == "true")
                {
                    drlStatus.SelectedValue= "True";
                }
                else
                {
                    drlStatus.SelectedValue = "False";
                }

                Image.ImageUrl = "~/images/upload/" + dr["image"].ToString();
      
            }

        }
    }
    protected void OK_Click(object sender, ImageClickEventArgs e)
    {
        int id = 0;
        string article_name = txtNameArticle.Text;
        string article_sumary = txtSumery.Text;
        string article_des = fckArticle.Value;
        string name_author = txtAuthor.Text;
        int category_id = int.Parse(drlCategory.SelectedItem.Value.ToString());
        int topic_id = int.Parse(drlTopic.SelectedItem.Value.ToString());
        int lang_id = int.Parse(drlLang.SelectedItem.Value.ToString());
        bool status = bool.Parse(drlStatus.SelectedItem.Value.ToString());
        string img = lblFileInfo.Text;
        if (Request.QueryString["path"] != "" && Request.QueryString["path"] != null)
        {
            id = Convert.ToInt32(Request.QueryString["path"]);
            art_BU.Update_Article_BU(id, article_name, article_sumary, article_des, img, category_id, topic_id, status, lang_id, name_author);
            Response.Redirect("default.aspx?action=Article");
        }
        else {
            art_BU.Update_Article_BU(id, article_name, article_sumary, article_des,img, category_id, topic_id, status, lang_id, name_author);
            Response.Redirect("default.aspx?action=Article");
        }
    }
    protected void Cancle_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("default.aspx?action=Article");
    }

    protected bool checkFile(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".jpg":
                return true;
            case ".bmp":
                return true;
            default:
                return false;
        }
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        string file = "";

        if (fileUp.HasFile)
        {
            if (checkFile(fileUp.FileName))
            {
                try
                {
                    file = fileUp.FileName.ToString().Trim();
                    //Response.Write("dsds" + file);
                    if (File.Exists(Request.MapPath("~") + "\\images\\upload\\" + Server.UrlEncode(file)))
                    {
                        file = fileUp.FileName.ToString().Trim();
                        Image.Visible = true;
                        Image.ImageUrl = "~/images/upload/" + Server.UrlEncode(file);

                        //Response.Write(file);
                    }
                    fileUp.SaveAs(Request.PhysicalApplicationPath + "images\\upload\\" + Server.UrlEncode(file));
                    Image.Visible = true;
                    Image.ImageUrl = "~/images/upload/" + Server.UrlEncode(file);
                    lblFileInfo.Text = fileUp.FileName;

                }
                catch (System.Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
    }
}
